package com.datn.activity;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.datn.utils.AlertCustom;
import com.datn.utils.BitmapUtils;
import com.datn.utils.PreferenceUtils;
import com.datn.utils.ScreenSizeUtils;
import com.datn.utils.places.DirectionObject;
import com.datn.utils.places.JsonDirection;
import com.datn.utils.places.Place;
import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class StaticGoogleMapActivity extends Activity implements OnClickListener{

	private Button btAdd, btShareViaEmail,btShareViaSMS;
	private TextView tvName;
	private Context mContext;
	public static final int PERCENT_DEFAUTL=80;
	public static final String COLOR_MARKER_DEFAULT="blue";
	private int width;//Width of static image
	private int height;//Height of static image
	private GeoPoint centerPositionImage;
	
	private Place placeReceived;
	
	//GET VALUE
	public GeoPoint getValueFromOtherActivity(){
		
		placeReceived = PreferenceUtils.getPlaceDetail();
		
		//Bundle extras = getIntent().getExtras();
		double latitude=0;
		double longitude=0;
		
		if(placeReceived !=null) {
			latitude = placeReceived.getLatitude();
			longitude = placeReceived.getLongtitude();
		}
		return new GeoPoint((int)(latitude*1E6), (int)(longitude*1E6));
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		//INIT
		setContentView(R.layout.static_google_map);
		mContext=this;		
		PreferenceUtils.getInstance(this);
		
		btAdd = (Button)findViewById(R.id.btAddStaticGoogleMap);
		btShareViaEmail = (Button)findViewById(R.id.btShareViaEmailStaticGoogleMap);
		btShareViaSMS = (Button)findViewById(R.id.btShareViaSMSStaticGoogleMap);
		tvName = (TextView)findViewById(R.id.tvNameStaticGoogleMap);
		
		ScreenSizeUtils size=new ScreenSizeUtils(mContext);
		size.setSizePercentFollowDevice(PERCENT_DEFAUTL);
		width = size.getWidth();
		height = size.getWidth();//size.getHeight();//getWidth for Square
		
		centerPositionImage = getValueFromOtherActivity();//Position for center Image
		getBitmapStaticGoogleMap(centerPositionImage);
		
		//Add click listener
		btAdd.setOnClickListener(this);
		btShareViaEmail.setOnClickListener(this);
		btShareViaSMS.setOnClickListener(this);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if (keyCode==KeyEvent.KEYCODE_BACK){
			Intent intent=new Intent(this, DetailsPlaceActivity.class);
			startActivity(intent);
			finish();
		}
		
		
		return super.onKeyDown(keyCode, event);
	}
	
	
	
	public void getBitmapStaticGoogleMap(final GeoPoint centerPositionImage){
		new AsyncTask<String, Void, Bitmap>() 
		{
			private ProgressDialog mProgressDialog;
			@Override
			protected void onPreExecute() 
			{
				mProgressDialog = new ProgressDialog(mContext);
				mProgressDialog.setMessage("Please Wait...");
				mProgressDialog.setCancelable(false);
				mProgressDialog.setIndeterminate(true);
				mProgressDialog.show();
			};

			@Override
			protected Bitmap doInBackground(String... args) {
				// TODO Auto-generated method stub
				return BitmapUtils.getStaticImageGoogleMap(centerPositionImage, width, height, COLOR_MARKER_DEFAULT);//Get Image from Google Map with size 200,200 and Color is green
			}

			@Override
			protected void onPostExecute(Bitmap bitmap) 
			{
				if (mProgressDialog.isShowing()) 
				{
					mProgressDialog.dismiss();
				}
				if (bitmap != null) 
					{
						ImageView imageView = (ImageView) findViewById(R.id.imageViewURL);
						imageView.setImageBitmap(bitmap);
						tvName.setText(placeReceived.getName());
					} 
				else 
					{
						/*AlertCustom.showAlert(mContext, "Error", "Cannot receive direction");*/
						Toast.makeText(mContext, "Cannot receive image place", Toast.LENGTH_LONG).show();
					}

			};
		}.execute("...");
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btAddStaticGoogleMap:
			AlertCustom.showAlert(this, "OK", "Add");
			break;
		case R.id.btShareViaEmailStaticGoogleMap:
			/*AlertCustom.showAlert(this, "OK", "Share");*/
			Intent i = new Intent(Intent.ACTION_SEND);  
			//i.setType("text/plain"); //use this line for testing in the emulator  
			i.setType("message/rfc822") ; // use from live device
			i.putExtra(Intent.EXTRA_EMAIL, new String[]{"@gmail.com"});  
			i.putExtra(Intent.EXTRA_SUBJECT,"Share location - "+placeReceived.getName());
			String stBodyEmail = "Name: "+ placeReceived.getName()+
					"\nAddress this place is: "+ placeReceived.getAddressLine()+
					"\nURL:"+ placeReceived.getActualurl() + 
					"\nPhone Number:"+ placeReceived.getPhonenumber() +					
					"\nYou can go to follow URL for see place in map:" + BitmapUtils.getURLStaticGoogleMap(centerPositionImage, width, height, COLOR_MARKER_DEFAULT) ;
			i.putExtra(Intent.EXTRA_TEXT, stBodyEmail);  
			startActivity(Intent.createChooser(i, "Select email application."));
			break;
		case R.id.btShareViaSMSStaticGoogleMap:
				Uri smsUri = Uri.parse("tel:");
				Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
				String stBodySMS = "Name: "+ placeReceived.getName()+
						"\nAddress this place is: "+ placeReceived.getAddressLine()+
						"\nURL:"+ placeReceived.getActualurl() + 
						"\nPhone Number:"+ placeReceived.getPhonenumber() +					
						"\nYou can go to follow URL for see place in map:\n" + BitmapUtils.getURLStaticGoogleMap(centerPositionImage, width, height, COLOR_MARKER_DEFAULT) ;
				intent.putExtra("sms_body", stBodySMS);
				intent.setType("vnd.android-dir/mms-sms");				
				startActivity(intent);
			break;
		default:
			break;
		}
	}
	
	
	
	
}
